<template>

  <div class="mod-config">
    <el-table
      :data="dataList"
      border
      style="width: 100%;">
      <el-table-column
        type="index"
        width="50"
      label="序号">
      </el-table-column>
      <el-table-column
        label="广告链接">
        <template slot-scope="scope">
          <el-input size="small" v-model="scope.row.url" placeholder="请输入内容"  @change="handleDelete(scope.$index, scope.row)" ></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="imgUrl"
        header-align="center"
        align="center"
        label="广告词">
        <template slot-scope="scope">
          <el-input size="small" v-model="scope.row.imgUrl" placeholder="请输入内容"  @change="handleDelete(scope.$index, scope.row)"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="fontColor"
        header-align="center"
        align="center"
        label="文字颜色"    width="80">
        <template slot-scope="scope">
          <el-color-picker v-model="scope.row.fontColor"  @active-change="activechange(this.val())"></el-color-picker>
        </template>
      </el-table-column>
      <el-table-column
        prop="imgUrl"
        header-align="center"
        align="center"
        label="边框颜色"    width="80">
        <template slot-scope="scope">
          <el-color-picker v-model="scope.row.boldColor" ></el-color-picker>
        </template>
      </el-table-column>
      <el-table-column
        prop="imgUrl"
        header-align="center"
        align="center"
        label="效果">
        <template slot-scope="scope">
          <el-checkbox v-model="scope.row.fontExpShan" >闪烁</el-checkbox><el-checkbox v-model="scope.row.fontExpBian" >边框</el-checkbox>
        </template>
      </el-table-column>
      <el-table-column
        prop="fontColor"
        header-align="center"
        align="center"
        label="字体大小"    width="80">
        <template slot-scope="scope">
          <el-input size="small" v-model="scope.row.fontSize" placeholder=""></el-input>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.$index, scope.row)">保存</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
  import API from '@/api'
  export default {
    data () {
      return {
        types: [{
          type: 1,
          label: '代码'
        }, {
          type: 2,
          label: '图片'
        }, {
          type: 3,
          label: '文字'
        }, {
          type: 4,
          label: '退弹'
        }],
        dataForm: {
          siteId: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        advaddOrUpdateVisible: false
      }
    },
    methods: {
      // 获取数据列表
      getDataListinit (siteId) {
        this.dataListLoading = true
        this.siteId = siteId
        var params = {
          page: this.pageIndex,
          limit: this.pageSize,
          siteId: siteId
        }
        API.basesiteadv.list(params).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      activechange (val) {
        console.log(val)
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.advaddOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.advAddOrUpdate.init(id, this.siteId)
        })
      },
      handleCurrentChange (row, event, column) {
        console.log(row, event, column, event.currentTarget)
      },
      saveAll () {
        for (let i = 0; i < this.dataList.length; i++) {
          var row = this.dataList[i]
          var params = {
            'id': row.id || undefined,
            'siteId': this.siteId,
            'title': row.title,
            'url': row.url,
            'imgUrl': row.imgUrl,
            'fontColor': row.fontColor,
            'boldColor': row.boldColor,
            'fontExpShan': row.fontExpShan === true ? 1 : 0,
            'fontExpBian': row.fontExpBian === true ? 1 : 0,
            'fontSize': row.fontSize,
            'status': row.status,
            'createUserId': row.createUserId,
            'createTime': row.createTime,
            'type': 3,
            'loca': row.loca,
            'orderNum': i
          }
     //     if (row.url !== null || row.imgUrl !== null) {
          var tick = !row.id ? API.basesiteadv.add(params) : API.basesiteadv.update(params)
          tick.then(({data}) => {
          })
      //    }
        }
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500,
          onClose: () => {
            this.visible = false
            this.getDataListinit(this.siteId)
          }
        })
      },
      handleEdit (index, row) {
        var params = {
          'id': row.id || undefined,
          'siteId': this.siteId,
          'title': row.title,
          'url': row.url,
          'imgUrl': row.imgUrl,
          'fontColor': row.fontColor,
          'boldColor': row.boldColor,
          'fontExpShan': row.fontExpShan === true ? 1 : 0,
          'fontExpBian': row.fontExpBian === true ? 1 : 0,
          'fontSize': row.fontSize,
          'status': row.status,
          'createUserId': row.createUserId,
          'createTime': row.createTime,
          'type': 3,
          'loca': row.loca,
          'orderNum': index
        }
        if (row.url !== null || row.imgUrl !== null) {
          var tick = !row.id ? API.basesiteadv.add(params) : API.basesiteadv.update(params)
          tick.then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.getDataListinit(this.siteId)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      },
      handleDelete (index, row) {
        if (row.id !== null && (row.url === null || row.url.length === 0) && (row.imgUrl === null || row.imgUrl.length === 0)) {
          var ids = row.id ? [row.id] : this.dataListSelections.map(item => {
            return item.id
          })
          API.basesiteadv.del(ids).then(({data}) => {
            if (data && data.code === 0) {
              console.log('success delete')
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          API.basesiteadv.del(ids).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataListinit(this.siteId)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
